package com.yfjin.ffmpeg54

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        reqPermission()
    }

    private fun reqPermission() {
        val permissions = mutableListOf<String>()
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
                getContext(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        ) {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
//        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
//                getContext(),
//                Manifest.permission.CAMERA
//            )
//        ) {
//            permissions.add(Manifest.permission.CAMERA);
//        }
        if (permissions.size != 0) {
            ActivityCompat.requestPermissions(this, permissions.toTypedArray(), 1000);
        } else {
            goToHome()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1000) {
            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
                    getContext(),
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
            ) {
                ToastUtil.show("请授权WRITE_EXTERNAL_STORAGE")
                return
            }
//            if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(
//                    getContext(),
//                    Manifest.permission.CAMERA
//                )
//            ) {
//                ToastUtil.show("请授权CAMERA")
//                return
//            }
            goToHome()
        }
    }


    fun getContext(): Context {
        return this
    }

    private fun goToHome() {
//        startActivity(Intent(getContext(), MainActivity::class.java))
//        startActivity(Intent(getContext(), PictureActivity::class.java))
//        startActivity(Intent(getContext(), PreviewActivity::class.java))
//                startActivity(Intent(getContext(), Camera2Activity::class.java))
        startActivity(Intent(getContext(), MainActivityAudio::class.java))
        finish()
    }
}